package com.eltonkent.db;

import java.util.List;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

abstract class GenericTable implements Persistable {
	private final int DB_VERSION = 17;
	private final String DB_NAME = "safe.db";
	private DBSetupWizard dbWizard;
	private SQLiteDatabase database;

	GenericTable() {
	}

	GenericTable(Context context) {
		dbWizard = new DBSetupWizard(context, DB_NAME, null, DB_VERSION);
		openDB();
	}

	void closeDB() {
		SQLiteDatabase.releaseMemory();
		if (database != null) {
			database.close();
		}
		if (dbWizard != null) {
			dbWizard.close();
		}
	}

	/**
	 * Used for any initial data that needs to be added to the table after its
	 * created.
	 * <p>
	 * <b>Important:</b><code>closeDB</code> should NOT be called before this
	 * function returns
	 * </p>
	 * 
	 * @param database
	 */
	abstract void doAfterTableCreate(SQLiteDatabase database);

	SQLiteDatabase getDatabase() {
		return database;
	}

	abstract List<Field> getTableFields();

	/**
	 * Should return the table name
	 * 
	 * @return
	 */
	abstract String getTableName();

	void openDB() {
		database = dbWizard.getWritableDatabase();
	}
}
